package net.boyandi.picback;

import java.awt.event.ActionEvent;
import java.util.List;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.BoxLayout;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import at.fhj.utils.misc.ProgressTracker;
import at.fhj.utils.swing.ProgressDialog;

/**
 * @author Ilya Boyandin
 */
public class BackupAction extends AbstractAction {

    private static final long serialVersionUID = -8607495358998805748L;

    private final PicBack app;

    public BackupAction(PicBack picBack) {
        this.app = picBack;

        putValue(Action.NAME, "Backup");
        putValue(Action.SMALL_ICON, PicBack.BACKUP_ICON);
        putValue(Action.SHORT_DESCRIPTION, "Backup selected folders");
        putValue(Action.LONG_DESCRIPTION, "Backup selected folders");
        putValue(Action.ACTION_COMMAND_KEY, "backup-command");     
    }
    
    public void actionPerformed(ActionEvent e) {
        List<String> folders = app.getSettings().getFoldersToScan();
        if (folders.size() == 0) {
            JOptionPane.showMessageDialog(app,
                    "You must first specify folders to scan (the folders should contain\n" +
                    "photos managed by Picasa) in the File / Settings menu.");
            return;
        }
        
        String dest = app.getSettings().getBackupDestinationFolder();
        if (dest == null  ||  dest.trim().length() == 0) {
            JOptionPane.showMessageDialog(app,
                    "You must first specify the destination folder for the backup\n" +
                    "in the File / Settings menu.");
            return;
        }
        
        int numFolders = app.getSelectedFolders().size();
        if (numFolders == 0) {
            JOptionPane.showMessageDialog(app,
                    "You must first select at least one album to backup.");
            return;
        }
        
        JPanel panel = new JPanel();
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
        panel.add(new JLabel("Copy " + (numFolders > 1 ? numFolders + " " : "") + "selected album" + 
                            (numFolders == 1 ? "" : "s") + " to '" + dest + "'?"));
//        panel.add(new JLabel("'" + dest + "'"));
        
        JCheckBox overwriteChk = new JCheckBox(
                "Overwrite destination files if older than source files (including Picasa.ini)");
        panel.add(overwriteChk);

        JCheckBox removeChk = new JCheckBox("Remove non-starred photos from the source albums after backup");
        panel.add(removeChk);
        
        int confirm = JOptionPane.showOptionDialog(
                app,
                panel,
                "Backup",
                JOptionPane.OK_CANCEL_OPTION,
                JOptionPane.QUESTION_MESSAGE, null, null, null
        );
        if (confirm == JOptionPane.OK_OPTION) {
            boolean removeNonStarred = removeChk.getModel().isSelected();
            boolean overwriteOlder = overwriteChk.getModel().isSelected();
            if (!removeNonStarred  ||
                 JOptionPane.showConfirmDialog(
                    app,
                    "Are you sure you want to remove non-starred photos from selected folders?",
                    "Confirm?",
                    JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION
            )
            startBackup(removeNonStarred, overwriteOlder);
        }
    }

    private void startBackup(boolean removeNonStarred, boolean overwriteOlder) {
        ProgressTracker progress = new ProgressTracker();
        Backuper worker = new Backuper(
                app, progress,
                removeNonStarred,
                overwriteOlder
        );
        ProgressDialog dialog = new ProgressDialog(app, "Backup", worker, false);
        progress.addProgressListener(dialog);
        worker.start();
        dialog.setVisible(true);
    }


}
